package main

import (
	"fmt"
	"reflect"
)

type Student01 struct {
	Name   string
	Age    int
	School string
}

func main() {
	s1 := Student01{Name: "喜羊羊", Age: 18, School: "大肥羊学校"}
	//通过反射，更改对象的数值，前提也是数据可以被修改
	fmt.Printf("%T\n", s1) //main.Student01
	p1 := &s1
	fmt.Printf("%T\n", p1) //*main.Student01
	fmt.Println(s1.Name)
	fmt.Println((*p1).Name, p1.Name)

	//改变数值
	value := reflect.ValueOf(&s1)
	if value.Kind() == reflect.Ptr {
		newValue := value.Elem()
		fmt.Println(newValue.CanSet()) //true

		f1 := newValue.FieldByName("Name")
		f1.SetString("灰太狼")
		f3 := newValue.FieldByName("School")
		f3.SetString("言传身教狼学校")
		fmt.Println(s1) //{灰太狼 18 言传身教狼学校}
	}
}
